﻿#include "RegisterDialog.h"
#include <Constants.h>
#include <Global_Class.h>
#include <users.h>
#include <QMessageBox>
#include <regex>

RegisterDialog::RegisterDialog(QWidget* parent) : QDialog(parent) {
    setWindowTitle("用户注册");
    QVBoxLayout* layout = new QVBoxLayout(this);
    infoLabel = new QLabel("请输入所有信息：", this);
    layout->addWidget(infoLabel);
    usernameEdit = new QLineEdit(this);
    usernameEdit->setPlaceholderText("用户名");
    layout->addWidget(usernameEdit);
    nicknameEdit = new QLineEdit(this);
    nicknameEdit->setPlaceholderText("昵称");
    layout->addWidget(nicknameEdit);
    passwordEdit = new QLineEdit(this);
    passwordEdit->setPlaceholderText("密码");
    passwordEdit->setEchoMode(QLineEdit::Password);
    layout->addWidget(passwordEdit);
    phoneEdit = new QLineEdit(this);
    phoneEdit->setPlaceholderText("手机号");
    layout->addWidget(phoneEdit);
    idCardEdit = new QLineEdit(this);
    idCardEdit->setPlaceholderText("身份证号");
    layout->addWidget(idCardEdit);
    btnRegister = new QPushButton("注册", this);
    layout->addWidget(btnRegister);
    connect(btnRegister, &QPushButton::clicked, this, &RegisterDialog::onRegisterClicked);
    setLayout(layout);
}

void RegisterDialog::onRegisterClicked() {
    std::string username = usernameEdit->text().toStdString();
    std::string nickname = nicknameEdit->text().toStdString();
    std::string password = passwordEdit->text().toStdString();
    std::string phone = phoneEdit->text().toStdString();
    std::string idCard = idCardEdit->text().toStdString();
    // 基本非空校验
    if (username.empty() || nickname.empty() || password.empty() || phone.empty() || idCard.empty()) {
        QMessageBox::warning(this, "注册失败", "所有信息均为必填项！");
        return;
    }
    // 用户名唯一性校验
    for (const auto& user : allData->usersQueue) {
        if (user.getUsername() == username) {
            QMessageBox::warning(this, "注册失败", "该用户名已被注册，请重新输入。");
            return;
        }
    }
    // 手机号格式校验（11位数字）
    std::regex phoneRegex("^1[3-9]\\d{9}$");
    if (!std::regex_match(phone, phoneRegex)) {
        QMessageBox::warning(this, "注册失败", "手机号格式不正确！");
        return;
    }
    // 身份证号格式校验（18位数字或17位数字+X/x）
    std::regex idCardRegex("^\\d{17}[\\dXx]$");
    if (!std::regex_match(idCard, idCardRegex)) {
        QMessageBox::warning(this, "注册失败", "身份证号格式不正确！");
        return;
    }
    // 身份证号唯一性校验
    for (const auto& user : allData->usersQueue) {
        if (user.getIdCard() == idCard) {
            QMessageBox::warning(this, "注册失败", "该身份证号已被注册，请直接登录或重新输入。");
            return;
        }
    }
    // 创建新用户
    users newUser;
    newUser.setUserId(std::to_string(allData->usersQueue.empty() ? 1 : std::stoi(allData->usersQueue.back().getUserId()) + 1));
    newUser.setUsername(username);
    newUser.setDisplayName(nickname);
    newUser.setPassword(password);
    newUser.setPhone(phone);
    newUser.setRole("customer");
    newUser.setIdCard(idCard);
	newUser.setState("1"); // 默认激活状态
    allData->usersQueue.push_back(newUser);
    allData->User_WriteToFile(Constants::USER_INFORMATION_FILE_POSITION);
    QMessageBox::information(this, "注册成功", "注册成功，请登录！");
    accept();
}
